﻿namespace Codeblack.TfsScrumboard.UI.Pages
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using Codeblack.TfsScrumboard.Resources;
    using Codeblack.TfsScrumboard.UI.Controls;
    using Codeblack.TfsScrumboard.ViewModels;
    using Microsoft.TeamFoundation.WebAccess.Common;
    using Microsoft.TeamFoundation.WebAccess.Controls;
    using Microsoft.TeamFoundation.WebAccess.UI;

    /// <summary>
    /// Represents the tasks page.
    /// </summary>
    public class TasksPage : ScrumboardPage
    {
        #region Control fields

        protected BacklogsControl backlogs;

        //protected WorkItemsControl workItems;

        #endregion

        #region WebAccess overrides

        /// <summary>
        /// Creates the breadcrumb, represented by an array of BreadCrumbSegment classes.
        /// </summary>
        /// <returns>An array of BreadCrumbSegment classes.</returns>
        protected override IEnumerable<BreadCrumbSegment> CreatePageBreadCrumb()
        {
            return new BreadCrumbSegment[]
            {
                new BreadCrumbSegment(ScrumboardResources.HomePageTitle, base.HomeUrl),
                new BreadCrumbSegment(ScrumboardResources.TasksPageTitle, this.Locator.CreateLinkUrl(base.ResolveUrl("~/UI/Pages/Scrumboard/Tasks.aspx"), string.Empty, new object[0]))
            };
        }

        /// <summary>
        /// Creates the page toolbar, represented by a StaticMenu class.
        /// </summary>
        /// <returns>An StaticMenu class containing the page toolbar.</returns>
        protected override StaticMenu CreatePageToolbar()
        {
            StaticMenu menu = null;

            if (base.Connected)
            {
                menu = ToolbarFactory.CreateToolbar(this, this, new ToolbarItems[]
                    {
                        ToolbarItems.New,
                        ToolbarItems.Separator,
                        ToolbarItems.Refresh,
                        ToolbarItems.Separator
                    });

                MenuItem menuRefresh = (from m in menu.Items where m.Id == "refresh" select m).FirstOrDefault();
                if (menuRefresh != null)
                {
                    menuRefresh.Action = "Scrumboard.refreshPlanning();";
                }

                MenuItem menuSelectBacklog = new MenuItem("Select Backlog", "toolbar-backlogs", this.ResolveUrl(TSWAConfiguration.Current.ImagesPath + "areas.gif"));
                menu.Items.Add(menuSelectBacklog);

                List<BacklogViewModel> selectBacklogs = new List<BacklogViewModel>();

                if (this.backlogs.Model != null && this.backlogs.Model.Project != null && this.backlogs.Model.Project.Backlogs != null)
                {
                    selectBacklogs.AddRange(this.backlogs.Model.Project.Backlogs);
                }
                if (this.backlogs.Model != null && this.backlogs.Model.Current != null && this.backlogs.Model.Current.Backlogs != null)
                {
                    selectBacklogs.AddRange(this.backlogs.Model.Current.Backlogs);
                }
                if (this.backlogs.Model != null && this.backlogs.Model.Future != null && this.backlogs.Model.Future.Backlogs != null)
                {
                    selectBacklogs.AddRange(this.backlogs.Model.Future.Backlogs);
                }

                foreach (BacklogViewModel backlog in selectBacklogs)
                {
                    menuSelectBacklog.ChildItems.Add(new MenuItem(backlog.Name, "toolbar-backlog", this.ResolveUrl(TSWAConfiguration.Current.ImagesPath + "areas.gif"), "Scrumboard.openPlanning('" + backlog.Name + "');"));
                }
            }

            return menu;
        }

        #endregion

        #region Page overrides

        /// <summary>
        /// Handles the beginning of page initialization.
        /// </summary>
        /// <param name="e">An System.EventArgs that contains the event data.</param>
        protected override void OnPreInit(EventArgs e)
        {
            this.ActiveTab = ScrumboardResources.TasksPage;
            this.Trace.Write(ScrumboardResources.TraceCategory, string.Format(CultureInfo.InvariantCulture, "Active Tab set to '{0}'", this.ActiveTab));

            base.OnPreInit(e);

            this.ContentTitle = ScrumboardResources.TasksPageTitle;
            this.Trace.Write(ScrumboardResources.TraceCategory, string.Format(CultureInfo.InvariantCulture, "Content Title set to '{0}'", this.ContentTitle));
        }

        /// <summary>
        /// Handles the System.Web.UI.Control.Load event.
        /// </summary>
        /// <param name="e">The System.EventArgs object that contains the event data.</param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            // retrieve the model containing the backlogs
            this.backlogs.Model = this.ScrumboardService.GetBacklogs(this.CurrentProject);

            // retrieve the model containing the work-items for the requested backlog
            //PlanningViewModel planningViewModel = this.ScrumboardService.GetPlanning(this.CurrentProject, this.BacklogName);

            // set the current backlog-name according to the model
            //this.BacklogName = planningViewModel.BacklogName;

            // TODO: change to WorkItemsViewModel
            //this.workItems.BacklogName = planningViewModel.BacklogName;
            //this.workItems.WorkItems = planningViewModel.WorkItems;
        }

        #endregion
    }
}
